package org.sample.traffic;

/**
 * Created by alan on 2017/7/11.
 */
public enum Lamp {

    N2S("南北", false, null, null),
    N2E("东北", false, null, null),
    N2W("西北", true, null, null),

    W2E("东西", false, null, null),
    W2N("西北", false, null, null),
    W2S("西南", true, null, null),

    S2N("南北", false, N2S, "S2W"),
    S2W("西南", false, N2E, "E2W"),
    S2E("东南", true, N2W, "S2E"),

    E2W("东西", false, W2E, "E2S"),
    E2S("东南", false, W2N, "S2N"),
    E2N("东北", true, W2S, "E2N");

    private String direction;

    private boolean lighted;

    private Lamp opposite;

    private String next;


    Lamp(String direction, boolean lighted, Lamp opposite, String next) {
        this.direction = direction;
        this.lighted = lighted;
        this.opposite = opposite;
        this.next = next;
    }

    public boolean isLighted() {
        return lighted;
    }

    public Lamp blackOut() {
        System.out.println(direction + "方向红灯亮");
        lighted = false;
        if (opposite != null)
            opposite.blackOut();

        Lamp next = null;
        if (this.next != null) {
            next = Lamp.valueOf(this.next);
            next.light();
        }
        return next;
    }

    public void light() {
        System.out.println(direction + "方向绿灯亮");
        lighted = true;
        if (opposite != null)
            opposite.light();
    }
}